package top.codedance.iotp.registy.net.handle.action;

import com.alibaba.fastjson.JSONObject;
import top.codedance.iotp.registy.RegistryApplication;
import top.codedance.iotp.registy.net.handle.AdapterController;

import java.util.Date;

public class ElectionController extends AdapterController {
    @Override
    public String doSubHandler(JSONObject body) {
        long id = body.getLongValue("id");
        boolean status = id > RegistryApplication.bootTimestamp;
        if(status){
            RegistryApplication.isMaster = false;
            RegistryApplication.isElectioning = false;
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("alive", status);
        Date d = new Date();
        RegistryApplication.syncLastTimestamp = d.getTime();
        return okResponse(jsonObject);
    }
}
